package com.aipodcast.webbackend.service.impl;

import com.aipodcast.webbackend.config.OssConfig;
import com.aipodcast.webbackend.config.model.ApiResponse;
import com.aipodcast.webbackend.entity.Courses;
import com.aipodcast.webbackend.entity.vo.CoursePageVo;
import com.aipodcast.webbackend.entity.vo.CourseVo;
import com.aipodcast.webbackend.mapper.CoursesMapper;
import com.aipodcast.webbackend.service.ICoursesService;
import com.aipodcast.webbackend.utils.OssUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-09-22
 */
@Service
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements ICoursesService {

    @Resource
    private OssUtil ossUtil;

    private static final String VIDEO_PATH_TEMPLATE = "video/%s";
    private static final String COVER_PATH_TEMPLATE = "cover/%s";

    @Override
    public CoursePageVo getCourseList(int pageNum, int pageSize) {
        Page<Courses> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        this.baseMapper.selectPage(page, null);
        CoursePageVo coursePageVo = new CoursePageVo();
        coursePageVo.setTotal(page.getTotal());
        coursePageVo.setPageNum(page.getPages());
        coursePageVo.setPageSize(page.getSize());
        coursePageVo.setCourseList(convert(page.getRecords()));

        return coursePageVo;
    }

    @Override
    public CoursePageVo getCourseListPublic(int pageNum, int pageSize) {
        Page<Courses> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getStatus, 1).orderBy(true, false, Courses::getSortOrder);
        this.baseMapper.selectPage(page, queryWrapper);
        CoursePageVo coursePageVo = new CoursePageVo();
        coursePageVo.setTotal(page.getTotal());
        coursePageVo.setPageNum(page.getPages());
        coursePageVo.setPageSize(page.getSize());
        coursePageVo.setCourseList(convert(page.getRecords()));
        return coursePageVo;
    }

    @Override
    public Courses getCourse(String id) {
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getId, id).eq(Courses::getStatus, 1);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public String getUploadSignUrl() {
        String fielPath = String.format(VIDEO_PATH_TEMPLATE, System.currentTimeMillis() + ".mp4");
        return ossUtil.getSignUrl(fielPath);
    }

    @Override
    public String uploadImg(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        String fileName = String.format(COVER_PATH_TEMPLATE, UUID.randomUUID().toString().replace("-", "") + ".jpg");

        return ossUtil.uploadFile(inputStream, fileName);

    }

    @Override
    public Boolean addCourse(String userId, CourseVo courseVo) {
        Courses courses = new Courses();
        BeanUtils.copyProperties(courseVo, courses);
        courses.setId(UUID.randomUUID().toString().replace("-", ""));
        courses.setCreatorId(userId);
        return this.save(courses);
    }

    private List<Courses> getCourseList(LambdaQueryWrapper<Courses> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    private List<CourseVo> convert(List<Courses> coursesList) {
        return coursesList.stream().map(this::convert).collect(Collectors.toList());
    }

    private CourseVo convert(Courses courses) {
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(courses, courseVo);
        return courseVo;
    }
}
